﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using PMisuseAn.io;

namespace PMisuseAn.behaviour.model
{
    public class AggregationStatementAnalyser
    {
        private static List<Attribute> agentAttributes = MisUseAnObjectsDB.loadAgentAttributes(MainWindow.clusterName);
        List<Attribute> environmentalAttributes = MisUseAnObjectsDB.loadEnvironmentAttributes(MainWindow.clusterName);
        List<Attribute> regulatoryAttributes = MisUseAnObjectsDB.loadRegulatoryAttributes(MainWindow.clusterName);

        private List<Attribute> attributesInStatement_;
        private Attribute premiseAttribute_;
        private List<Attribute> conclusionAttributes_;

        bool isFirstAttribute = false;

        public AggregationStatementAnalyser(string aggregationStatement)
        {
            attributesInStatement_ = new List<Attribute>();
            conclusionAttributes_ = new List<Attribute>();

            aggregationStatementTokenisation(aggregationStatement);
        }

        private void aggregationStatementTokenisation(string aggregationStatement)
        {
            string[] words21 = Regex.Split(aggregationStatement, " ");
            foreach (string word in words21)
            {
                if (word != null || !word.Trim().Equals(""))
                {
                    Attribute attribute = getAttribute(word);
                    if (isFirstAttribute == true)
                    {
                        premiseAttribute_ = attribute;
                    }
                    else
                    {
                        conclusionAttributes_.Add(attribute);
                    }

                    if (attribute != null)
                    {
                        attributesInStatement_.Add(attribute);
                    }
                }
            }

        }

        private Attribute getAttribute(string word)
        {
            Attribute attribute = null;
            foreach (Attribute att in agentAttributes)
            {
                if (att.name.Equals(word))
                {
                    attribute = att;
                    return attribute;
                }
            }

            foreach (Attribute att in environmentalAttributes)
            {
                if (att.name.Equals(word))
                {
                    attribute = att;
                    return attribute;
                }
            }

            foreach (Attribute att in regulatoryAttributes)
            {
                if (att.name.Equals(word))
                {
                    attribute = att;
                    return attribute;
                }
            }

            return attribute;
        }

        public List<Attribute> attributesInStatement
        {
            get
            {
                return attributesInStatement_;
            }
        }

        public Attribute premiseAttribute
        {
            get
            {
                return premiseAttribute_;
            }
        }

        public List<Attribute> conclusionAttributes
        {
            get
            {
                return conclusionAttributes_;
            }
        }


    }
}
